# -*- coding: utf-8 -*-

#import cv2
import time,PIL,os
from PIL import ImageGrab
import numpy as np
import time
from tempfile import NamedTemporaryFile
import easyocr
from concurrent.futures import ThreadPoolExecutor
import sys

#截图
class ScreenCut:
    def __init__(self):
        if not os.path.exists("D:/curelasto/tmp/"):
            os.makedirs("D:/curelasto/tmp/")
        try:
            os.remove("D:/curelasto/tmp/scr.png")
            os.remove("D:/curelasto/tmp/cut.png")
        except:pass
        self.scrImg = open("D:/curelasto/tmp/scr.png","wb+")
        self.cutImg = open("D:/curelasto/tmp/cut.png","wb+")
    def cut(self):
        self.scrren_cut()
        #print("截图完成")
    def scrren_cut(self):
         # 获取传递给Python脚本的参数
        arg1 = sys.argv[1]
        arg2 = sys.argv[2]
        x = int(arg1)  # 将第一个参数转换为整数
        y = int(arg2)  # 将第二个参数转换为整数
        #print("x is:", x)
        #print("y is:", y)
        # img = ImageGrab.grab(bbox=(
        #         1000,       # 起始x坐标
        #         800,       # 起始y坐标
        #         1000+500,  # 起始x + 宽度
        #         800+200   # 起始y + 高度
        #     ))
        img = ImageGrab.grab(bbox=(
                x,       # 起始x坐标
                y,       # 起始y坐标
                x+160,  # 起始x + 宽度
                y+50   # 起始y + 高度
            ))

        img.save(self.cutImg.name)


cut = ScreenCut()
cut.cut()

reader = easyocr.Reader(['en'])
# 支持中文简体/繁体+英文
#reader = easyocr.Reader(['ch_sim','ch_tra','en'])
result = reader.readtext("D:/curelasto/tmp/cut.png")

if not result:
    #print("未识别到文字")
    print("Text not recognized>>>>>>>")
    exit()

#extracted_text = []
extracted_text = ''
for detection in result:
    #extracted_text.append(detection[1])
    #print(detection[1])
    extracted_text += detection[1]

# 可选：移除所有括号（如需保留请删除此段）
extracted_text = extracted_text.replace('(', '').replace(')', '')
print(extracted_text)